﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WeiXin.SDK.Entities;
using WeiXin.SDK.Entities.Menu;

namespace WeiXin.SDK
{
    public class CustomMenuAPI
    {
        /// <summary>
        /// 自定义菜单创建接口
        /// </summary>
        /// <param name="token"></param>
        /// <param name="content"></param>
        /// <returns></returns>
        public static WxJsonResult Create(string token, GetMenuResultFull menuResult)
        {
            var newMenu = ProcessMenu(menuResult);
            string menuJson = Newtonsoft.Json.JsonConvert.SerializeObject(newMenu.menu);

            var result = HttpClientHelper.PostResponse<WxJsonResult>(string.Format("https://api.weixin.qq.com/cgi-bin/menu/create?access_token={0}", token), menuJson);
            return result;

        }

        private static GetMenuResultFull ProcessMenu(GetMenuResultFull menuResult)
        {
            MenuFull_ButtonGroup newButtonGroup = new MenuFull_ButtonGroup();

            var rootButtonList = menuResult.menu.button;
            foreach (var rootButton in rootButtonList)
            {
                if (string.IsNullOrEmpty(rootButton.name))
                {
                    continue;//没有设置以及菜单
                }
                //二级菜单的按钮数量
                var availableSubButton = rootButton.sub_button == null ? 0 : rootButton.sub_button.Count(m => !string.IsNullOrEmpty(m.name));
                if (availableSubButton == 0)
                {
                    #region 底部单击按钮

                    if (rootButton.type == null ||
                        (rootButton.type.Equals("CLICK", StringComparison.OrdinalIgnoreCase) && string.IsNullOrEmpty(rootButton.key))
                     )
                    {
                        throw new Exception("单击按钮的key不能为空");
                    }
                    newButtonGroup.button.Add(new MenuFull_RootButton()
                    {
                        name = rootButton.name,
                        key = rootButton.key,
                        url = rootButton.url,
                        type = rootButton.type
                    });
                    #endregion
                }
                else
                {
                    #region 二级菜单
                    MenuFull_RootButton newParentButton = new MenuFull_RootButton()
                    {
                        name = rootButton.name,
                        key = rootButton.key,
                        url = rootButton.url,
                        type = rootButton.type
                    };

                    foreach (var subButton in rootButton.sub_button)
                    {
                        if (string.IsNullOrEmpty(subButton.name))
                        {
                            continue;
                        }
                        if (subButton.type == null ||
                            (subButton.type.Equals("CLICK", StringComparison.OrdinalIgnoreCase) && string.IsNullOrEmpty(subButton.key))
                        )
                        {
                            throw new Exception("单击按钮的key不能为空");
                        }

                        newParentButton.sub_button.Add(subButton);
                    }

                    newButtonGroup.button.Add(newParentButton);
                    #endregion

                }
            }

            return new GetMenuResultFull()
            {
                menu = newButtonGroup
            };

        }


        /// <summary>
        /// 自定义菜单查询接口
        /// </summary>
        /// <param name="token"></param>
        /// <returns></returns>
        public static GetMenuResultFull Query(string token)
        {
            var resultJson = HttpClientHelper.GetResponse<GetMenuResultFull>(string.Format("https://api.weixin.qq.com/cgi-bin/menu/get?access_token={0}", token));
            if (resultJson.errcode != ReturnCode.请求成功)
            {
                throw new Exception(resultJson.ToString());
            }
            else
            {
                return resultJson;
            }
        }


        /// <summary>
        /// 自定义菜单删除接口
        /// </summary>
        /// <param name="token"></param>
        /// <returns></returns>
        public static bool Delete(string token)
        {

            var result = HttpClientHelper.GetResponse<WxJsonResult>(string.Format("https://api.weixin.qq.com/cgi-bin/menu/delete?access_token={0}", token));
            return result.errcode == 0;


        }
    }
}
